package com.ww.user.controller;

import com.querydsl.jpa.impl.JPAQueryFactory;
import com.ww.common.ServiceResponse;
import com.ww.orm.entity.QUserEntity;
import com.ww.orm.entity.UserEntity;
import com.ww.orm.respository.UserRespostory;
import org.activiti.engine.IdentityService;
import org.activiti.engine.identity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author sunnannan
 * @version 1.0
 * @date 2020/11/26 11:48 上午
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private JPAQueryFactory jpaQueryFactory;
    @Autowired
    private UserRespostory userRespostory;
    @Autowired
    private IdentityService identityService;

    @PostMapping
    public ServiceResponse saveUser(@RequestBody UserEntity userEntity){
        UserEntity save = userRespostory.save(userEntity);
        User user = identityService.newUser("nan");
        user.setId(String.valueOf(save.getId()));
        user.setFirstName(save.getUserName());
        identityService.saveUser(user);
        return ServiceResponse.success(save);
    }
    @GetMapping
    public ServiceResponse getUserById(@RequestParam Long userId){
        return ServiceResponse.success(userRespostory.findById(userId));
    }
    @GetMapping(value = "list")
    public ServiceResponse getUserWhihPage(@RequestParam(value = "rowSize", defaultValue = "1000", required = false) Integer rowSize, @RequestParam(value = "page", defaultValue = "1", required = false) Integer page){
        page = page < 0?0:page;
        return ServiceResponse.success(jpaQueryFactory.selectFrom(QUserEntity.userEntity).offset(page * rowSize).limit(rowSize).fetch());
    }
}
